package com.itheima.day17.xmlparse.test1;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;

public class Test2 {
    public static void main(String[] args) throws DocumentException {
        // 获取解析器对象
        SAXReader saxReader = new SAXReader();

        // 解析xml文件，返回文档对象
        Document document = saxReader.read("myCode\\xml\\student.xml");

        // 获取根标签
        Element rootElement = document.getRootElement();

        // 获取所有student标签
        List<Element> studentElements = rootElement.elements("student");

        // 遍历
        for (Element element : studentElements) {
            // 获得id值
            Attribute attribute = element.attribute("id");
            String id = attribute.getValue();

            // 获取name值
            Element nameElement = element.element("name");
            String name = nameElement.getText();

            // 获取age值
            Element ageElement = element.element("age");
            String age = ageElement.getText();

            System.out.println(id);
            System.out.println(name);
            System.out.println(age);
        }
    }
}
